package com.itmk.web.reports.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.reports.entity.ReportListParm;
import com.itmk.web.reports.entity.ReportVo;
import com.itmk.web.reports.entity.Reports;
import com.itmk.web.reports.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/reports")
public class ReportController {

    @Autowired
    private ReportService reportService;

    //    投诉接口
    @PostMapping("/report")
    public ResultVo report(@RequestBody Reports reports) {
        // 参数校验
        if (reports.getGoodsId() == null || reports.getUserId() == null
                || reports.getReportType() == null || StringUtils.isEmpty(reports.getReason())) {
            return ResultUtils.error("参数不完整");
        }
        //        判断是否投诉
        QueryWrapper<Reports> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Reports::getGoodsId, reports.getGoodsId())
                .eq(Reports::getUserId, reports.getUserId());
        Reports existingReport = reportService.getOne(queryWrapper);
        if (existingReport != null) {
            return ResultUtils.error("已投诉");
        }
        reports.setCreatedAt(new Date());
        if (reportService.save(reports)) {
            return ResultUtils.success("投诉成功");
        }
        return ResultUtils.error("投诉失败");
    }

    //    投诉接口
    @GetMapping("/isReport")
    public ResultVo isReport(int goodsId, int userId) {
        QueryWrapper<Reports> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Reports::getGoodsId, goodsId)
                .eq(Reports::getUserId, userId);
        Reports existingReport = reportService.getOne(queryWrapper);
        if (existingReport != null) {
            return ResultUtils.error("已经投诉过", 1);
        } else {
            return ResultUtils.error("未投诉", 0);
        }

    }
//列表查询
    @GetMapping("getReportList")
    public ResultVo getReportList(ReportListParm parm){
        IPage<ReportVo> list = reportService.getReportList(parm);
        return ResultUtils.success("get report list success",list);
    }

//    管理员处理投诉内容
    @PostMapping("handleReport")
    public ResultVo handleReport(@RequestBody Reports reports){
        UpdateWrapper<Reports> updateWrapper =new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(Reports::getStatus, 1)  // 将status从0改为1
                .eq(Reports::getReportId, reports.getReportId());

        if(reportService.update(updateWrapper)){
            return ResultUtils.success("admin handle success");
        }
        return ResultUtils.error("admin handle error");

    }
}
